var util = require("../../../utils/util.js"),
  api = require("../../../utils/api.js"),
  app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    src: {
      type: String,
      value: '',
      observer(newVal) {
        this.setData({headimgurl:newVal})
      }
    }
  },
  ready: function() {
    // 在组件实例进入页面节点树时执行
   
  },

  /**
   * 组件的初始数据
   */
  data: {
    headimgurl:'',
    baseImgUrl: app.globalData.baseImgUrl,
    show:false,
    src: '',
    width: 200, //宽度
    height: 200, //高度
  },

  /**
   * 组件的方法列表
   */
  methods: {
    cropperload(e) {
      console.log("cropper初始化完成");
      this.cropper = this.selectComponent("#image-cropper");
    },
    loadimage(e) {
      console.log("图片加载完成", e.detail);
      wx.hideLoading();
      console.log('this.cropper',this.cropper)
      //重置图片角度、缩放、位置
      this.cropper.imgReset();
    },
    successBtn(e){
      wx.showLoading();
      this.cropper.getImg(res=>{
        console.log('res',res);
        if(res.url){
          wx.showLoading({
            title: '上传中',
            mask: true
          })
          this.headUploadImgs(res.url);
        }
      })
    },
    addImage(e) {
      wx.navigateTo({
        url: '/pages/mine/headerImage/headerImage',
      })
    },
    headUploadImgs(filePath) {
      wx.uploadFile({
        url: app.globalData.baseUrl + 'common/index/uploadImg', //仅为示例，非真实的接口地址//接口连接
        filePath: filePath,
        name: 'file',
        success: (res) => {
          if (res.data) {
            let json = JSON.parse(res.data)
            if (json.code == 1) {
              this.headImgWrite(json.data)
            } else {
              wx.showToast({
                title: '上传失败',
                icon: 'none'
              })
            }
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'none'
            })
          }
        }
      })
    },
    headImgWrite(url) {
      api.default.setMemberInfo({
        headimgurl: url,
        flag: 2
      }).then(res => {
        wx.hideLoading()
        wx.showToast({
          title: '上传成功！',
          icon: 'success'
        })
        this.setData({headimgurl:url,show:false})
        this.triggerEvent('callback', {headimgurl:url})
      })
    },
    closeMethod(){
      this.setData({show:false})
    },
    showImage: function (event) {
      const {
        src,
        list
      } = event.currentTarget.dataset;
      wx.previewImage({
        current: src,
        urls: Array.isArray(list) ? list.map(o => this.data.baseImgUrl + o) : [src]
      })
    }
  }
})